package com.mybatis.demo.mapper;

import com.mybatis.demo.model.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserInfoMapper {

    //@Select("select * from user_info")
//    @Select("SELECT id,username,password,age,gender,phone," +
//            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime FROM `user_info`")
    @Results(id = "BaseMap",value = {
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    }
    )
    @Select("select * from user_info")
    List<UserInfo> selectAll();

    @ResultMap(value = "BaseMap")
    @Select("select * from user_info where id = #{id}")
    UserInfo selectAllById(Integer id);

    @ResultMap(value = "BaseMap")
    @Select("select * from user_info where id = ${id}")
    UserInfo selectAllById2(Integer id);

    @Select("select * from user_info where username = #{userName} and password = #{password}")
    List<UserInfo> selectAllByNameAndPassword(@Param("userName")String username, @Param("password") String password);

    @Select("select * from user_info where username = '${userName}' and password = '${password}'")
    List<UserInfo> selectAllByNameAndPassword2(@Param("userName")String username, @Param("password") String password);

    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age) values(#{username},#{password},#{age})")
    Integer insertUser(UserInfo userInfo);


    @Options(useGeneratedKeys = true,keyProperty = "id")
//    @Insert("insert into user_info(username,password,age) values(#{userInfo.username}," +
//            "#{userInfo.password},#{userInfo.age})")
    @Insert("insert into user_info(username,password,age) values(#{username},#{password},#{age})")
    Integer insertUser2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete from user_info where id = #{id}")
    Integer deleteUser(Integer id);

    @Update("update user_info set delete_flag = #{deleteFlag},phone = #{phone} where id = #{id}")
    Integer updataUser(UserInfo userInfo);

    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserInfoByOrder(String order);

    //@Select("select * from user_info where username like '%${userName}%'")
    @Select("select * from user_info where username like concat('%',#{userName},'%')")
    List<UserInfo> selectUserInfoByLike(String userName);

    @Insert("<script>" +
            "insert into user_info " +
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">" +
            "<if test='username != null'> username,</if>" +
            "<if test='password != null'> password,</if>" +
            "<if test='gender != null'> gender,</if>" +
            "<if test='phone != null'> phone,</if>" +
            "<if test='age != null'> age,</if>" +
            "</trim> values" +
            "<trim prefixOverrides=\",\" suffixOverrides=\",\" prefix=\"(\" suffix=\")\">" +
            "<if test='username != null'> #{username},</if>" +
            "<if test='password != null'> #{password},</if>" +
            "<if test='gender != null'> #{gender},</if>" +
            "<if test='phone != null'> #{phone},</if>" +
            "<if test='age != null'> #{age}</if>" +
            "</trim>" +
            "</script>")
    Integer insertUser3(UserInfo userInfo);
}
